//
//  ViewController.swift
//  swiftAnimation
//
//  Created by zhouLiHuan on 2017/2/16.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    var titleArr: [[[NSString : String]]] = [[["clsName":"BaseAnimViewController","title":"CABasecAnimation基础动画"],
                                              ["clsName":"DrawLineViewController","title":"画线动画"],
                                              ["clsName":"FireViewController","title":"粒子动画-火苗效果"],
                                              ["clsName":"KeyFrameViewController","title":"CAKeyFrameAnimation 基础动画"],
                                              ["clsName":"SpringViewController","title":"CASpringAnimation弹簧动画"],
                                              ["clsName":"TransiitionViewController","title":"CATransition转场动画"],
                                              ["clsName":"WaterWaveViewController","title":"水波纹效果"]]
    ];
    

    var sectionTitleArr = ["动画基础示例","动画常见经典案例"];
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        let tableView = UITableView(frame: view.bounds, style: .grouped)
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }
}


extension ViewController:UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return titleArr.count;
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let tempArr = self.titleArr[section]
        return tempArr.count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let tempArr = titleArr[indexPath.section];
        let tempDic = tempArr[indexPath.row];
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath);
        cell.textLabel?.text = tempDic["title"];
        return cell;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let tempArr = titleArr[indexPath.section];
        let tempDic = tempArr[indexPath.row];
        let nameSpace = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String
        guard let clsName = tempDic["clsName"],
            let cls = NSClassFromString(nameSpace + "." + clsName) as?UIViewController.Type else{
                return;
        }
        let vc  = cls.init();
        vc.title = tempDic["title"];
        navigationController?.pushViewController(vc, animated: true)
        func tableView(_ tableView: UITableView, titleForHeaderInSection section:Int) ->String?{
            return sectionTitleArr[section]
        }
     
    }
}















